//
//  PersonView.swift
//  maosheng
//
//  Created by lxl on 2019/8/15.
//

import UIKit
typealias ClickBtn = (_ tag : Int) -> Void
class PersonView: UIView {
    let userImg = UIImageView()
    let userNameLab = UILabel()
    let topBtn = UIButton(type: .custom)//跳转到个人主页的
    var didClickBtn : ClickBtn?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setPersonView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setPersonView()  {
        let imgHeight = 75
        userImg.layer.cornerRadius = CGFloat(imgHeight/2)
        userImg.layer.masksToBounds = true
        userImg.contentMode = .scaleAspectFit
        userImg.image = UIImage(named: "login_head_moren")
        self.addSubview(userImg)
        userImg.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.width.height.equalTo(imgHeight)
            make.top.equalTo(55)
        }
        
        addSubview(userNameLab)
        userNameLab.text =  "未设置昵称"

        userNameLab.textAlignment = .left
        userNameLab.textColor = KYP_333333_COLOR
        userNameLab.font = UIFont(name: PF_Medium, size: 22)
        userNameLab.snp.makeConstraints { (make) in
            make.left.equalTo(userImg.snp.right).offset(15)
            make.top.equalTo(userImg).offset(15)
        }
        
        let rightImg = UIImageView(image: UIImage(named: "edit_right"))
        addSubview(rightImg)
        rightImg.snp.makeConstraints { (make) in
            make.right.equalTo(-15)
            make.bottom.equalTo(userImg.snp.centerY)
        }
        let rightLab = UILabel()
        rightLab.text = "我的主页"
        addSubview(rightLab)
        rightLab.textColor = KYP_999999_COLOR
        rightLab.font = UIFont(name: PF_Medium, size: 14)
        rightLab.snp.makeConstraints { (make) in
            make.right.equalTo(rightImg.snp.left).offset(-7)
            make.centerY.equalTo(rightImg)
        }
        
        addSubview(topBtn)
        topBtn.snp.makeConstraints { (make) in
            make.top.equalTo(30)
            make.right.left.equalTo(0)
            make.bottom.equalTo(userImg)
        }
        
        var tmpLab : UILabel?
        let arr_title = ["关注","粉丝","收藏","最近浏览"]
        let LabWidth = SCREENW/CGFloat(arr_title.count)
        for i in 0..<arr_title.count {
            let titleLab = UILabel()
            titleLab.textAlignment = .center
            titleLab.tag = 101 + i
            titleLab.text = "0"
            addSubview(titleLab)
            titleLab.textColor = KYP_333333_COLOR
            titleLab.font = UIFont(name: PF_Medium, size: 17)
            titleLab.snp.makeConstraints { (make) in
                make.width.equalTo(LabWidth)
                make.top.equalTo(userImg.snp.bottom).offset(50)
                if tmpLab != nil{
                    make.left.equalTo(tmpLab!.snp.right).offset(0)
                }else{
                    make.left.equalTo(0)
                }
            }
            
            let descLab = UILabel()
            descLab.text = arr_title[i]
            addSubview(descLab)
            descLab.textAlignment = .center
            descLab.textColor = KYP_666666_COLOR
            descLab.font = UIFont(name: PF_Regular, size: 13)
            descLab.snp.makeConstraints { (make) in
                make.width.left.equalTo(titleLab)
                make.top.equalTo(titleLab.snp.bottom).offset(8)
            }
            
            let lineView = UIView()
            addSubview(lineView)
            lineView.backgroundColor = KYP_999999_COLOR
            lineView.snp.makeConstraints { (make) in
                make.left.equalTo(titleLab.snp.right).offset(-0.5)
                make.top.equalTo(titleLab).offset(6)
                make.width.equalTo(0.5)
                make.height.equalTo(20)
            }
            
            
            tmpLab = descLab
            
            let centerBtn = UIButton()
            centerBtn.tag = 201 + i
            centerBtn.addTarget(self, action: #selector(topBtnClick(btn:)), for: .touchUpInside)
            addSubview(centerBtn)
            centerBtn.snp.makeConstraints { (make) in
                make.top.left.right.equalTo(titleLab)
                make.bottom.equalTo(descLab).offset(22)
            }
            
        }
        
        let centerLineView = UIView()
        addSubview(centerLineView)
        centerLineView.backgroundColor = TABLE_LINE_COLOR
        centerLineView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(tmpLab!.snp.bottom).offset(22)
            make.height.equalTo(10)
        }
        
        let centerView = UIView()
        addSubview(centerView)
        centerView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(centerLineView.snp.bottom)
            make.height.equalTo(100)
        }
        
        
        let arr_center = ["我的评论","我的点赞"]
        let arr_centerImg = ["my_1","my_2"]

        for i in 0..<arr_center.count {
            let centerBtn = UIButton(type: .custom)
            centerBtn.tag = 301 + i
            centerBtn.addTarget(self, action: #selector(centerJump(btn:)), for: .touchUpInside)
            centerBtn.setTitleColor(KYP_333333_COLOR, for: .normal)
            centerBtn.setTitle(arr_center[i], for: .normal)
            centerBtn.setImage(UIImage(named: arr_centerImg[i]), for: .normal)
            centerBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
            centerView.addSubview(centerBtn)
            centerBtn.snp.makeConstraints { (make) in
                make.top.bottom.equalTo(0)
                make.left.equalTo(LabWidth * CGFloat(i))
                make.width.equalTo(80)
            }
            centerView.layoutIfNeeded()
            changeTheBtnImgAndTitle(button: centerBtn)
        }
        
        
        let bottomLineView = UIView()
        addSubview(bottomLineView)
        bottomLineView.backgroundColor = TABLE_LINE_COLOR
        bottomLineView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(centerView.snp.bottom)
            make.height.equalTo(10)
        }
        
        let bottomView = UIView()
        addSubview(bottomView)
        bottomView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(bottomLineView.snp.bottom)
            make.height.equalTo(100)
        }
        
        
        let arr_bottom = ["我的客服","商务合作","检测更新","系统设置"]
        let arr_bottomImg = ["my_3","my_4","my_5","my_6"]

        for i in 0..<arr_bottom.count {
            let centerBtn = UIButton(type: .custom)
            centerBtn.tag = 401 + i
            centerBtn.addTarget(self, action: #selector(bottomJump(btn:)), for: .touchUpInside)
            centerBtn.setTitleColor(KYP_333333_COLOR, for: .normal)
            centerBtn.setTitle(arr_bottom[i], for: .normal)
            centerBtn.setImage(UIImage(named: arr_bottomImg[i]), for: .normal)
            centerBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
            bottomView.addSubview(centerBtn)
            centerBtn.snp.makeConstraints { (make) in
                make.top.bottom.equalTo(0)
                make.left.equalTo(LabWidth * CGFloat(i))
                make.width.equalTo(80)
            }
            bottomView.layoutIfNeeded()
            changeTheBtnImgAndTitle(button: centerBtn)

        }
        
    }
    
    @objc func topBtnClick(btn:UIButton){
        if self.didClickBtn != nil {
            self.didClickBtn!(btn.tag)
        }
    }
    //
    @objc func centerJump(btn:UIButton) -> Void {
        if self.didClickBtn != nil {
            self.didClickBtn!(btn.tag)
        }
    }
    
    @objc func bottomJump(btn:UIButton) -> Void {
        if self.didClickBtn != nil {
            self.didClickBtn!(btn.tag)
        }
    }
    
    
    func changeTheBtnImgAndTitle(button:UIButton) -> Void {
        let offset = 15.0
        
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: -button.imageView!.frame.size.width, bottom: -button.imageView!.frame.size.height - CGFloat(offset/2), right: 0)
        button.imageEdgeInsets = UIEdgeInsets(top: -(button.titleLabel?.intrinsicContentSize.height)! - CGFloat(offset/2), left: 0, bottom: 0, right: -(button.titleLabel?.intrinsicContentSize.width)!)
      
    }
    
    func setPersonViewInfo(model : PersonModel) -> Void {
        setLabWith(tag: 101, title: model.user?.followedCount)
        setLabWith(tag: 102, title: model.user?.followerCount)
        setLabWith(tag: 103, title: model.user?.favoriteCount)
        setLabWith(tag: 104, title: model.user?.viewCount)
        
        userNameLab.text = model.user?.nickName != nil ?  model.user?.nickName : "未设置昵称"
        if let str_url = model.user?.avatar?.url{
            userImg.kf.setImage(with: URL(string: str_url),placeholder:UIImage(named: "login_head_moren") )
        }
    }
    func setLabWith(tag : Int , title : String?) -> Void{
        if let tmpLb = viewWithTag(tag) as? UILabel{
            
            tmpLb.text = title != nil ? title : "0"
        }
    }
}
